package com.simple.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.simple.gulimall.product.entity.ProductAttrValueEntity;
import com.simple.gulimall.product.service.ProductAttrValueService;
import com.simple.gulimall.product.vo.AttrRespVo;
import com.simple.gulimall.product.vo.AttrVo;
import org.springframework.web.bind.annotation.*;

import com.simple.gulimall.product.service.AttrService;
import com.simple.common.utils.PageUtils;
import com.simple.common.utils.R;

import javax.annotation.Resource;


/**
 * 商品属性
 *
 * @author simpleZzz
 * @email 2466714892@qq.com
 * @date 2020-07-04 00:47:29
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Resource
    private AttrService attrService;
    @Resource
    ProductAttrValueService productAttrValueService;

    /**
     * 基本列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 查询关联数据
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseList(@RequestParam Map<String, Object> params,
                      @PathVariable("catelogId") Long catelogId,
                      @PathVariable("attrType") String attrType){
        PageUtils page = attrService.queryBaseAttrPage(params,catelogId,attrType);

        return R.ok().put("page", page);
    }

    // /product/attr/base/listforspu/{spuId}
    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrListForSpu(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> productAttrValueEntities = productAttrValueService.baseAttrListForSpu(spuId);

        return R.ok().put("data",productAttrValueEntities);
    }
    // /product/attr/update/{spuId}
    @PostMapping("/update/{spuId}")
    public R updateAttr(@PathVariable("spuId") Long spuId,@RequestBody List<ProductAttrValueEntity> entity){
        productAttrValueService.updateSpuAttr(spuId,entity);

        return R.ok();
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
		AttrRespVo attrRespVo = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attrRespVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
